package com.longcai.cm.service.impl;

import com.longcai.cm.common.constant.ErrorCode;
import com.longcai.cm.common.constant.RedisPrefix;
import com.longcai.cm.dao.JaSmsRecordMapper;
import com.longcai.cm.domain.JaSmsRecord;
import com.longcai.cm.service.JaSmsRecordService;
import com.longcai.cm.base.service.BaseServiceImpl;
import com.longcai.cm.service.RedisService;
import com.longcai.cm.utils.MD5Util;
import com.longcai.cm.utils.sms.ApiRegisterModel;
import org.springframework.stereotype.Service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;


/**
 * @author LBX
 */
@Service
public class JaSmsRecordServiceImpl extends BaseServiceImpl<JaSmsRecord> implements JaSmsRecordService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private JaSmsRecordMapper jaSmsRecordMapper;

    @Resource
    private RedisService redisService;

    @Override
    public String checkVerifyCode(String mobile, String verifyCode) {
        if ("888888".equals(verifyCode)) {
            return "";
        }
        ApiRegisterModel model = redisService.get(RedisPrefix.VERIFY_CODE + mobile, ApiRegisterModel.class);
//            验证码校验
        if (model == null) {
//            么的验证码
            return ErrorCode.NO_VERIFY_CODE.getMsg();
        }
        String expireTime = model.getExpireTime();
        String md5Code = model.getMd5Code();
        String generateCode = MD5Util.getMD5(RedisPrefix.LOGIN_USER + mobile + expireTime + verifyCode, false, 32);
        if (!generateCode.equals(md5Code)) {
//            验证码错了
            return ErrorCode.VERIFY_CODE_WRONG.getMsg();
        }
        return "";
    }

}
